package com.study.concurrent.A_ThreadManagement.c_ThreadInterrupt;

/**
 * Created by 赵超 on 2017/3/18.
 */
public class PrimeGenerator extends Thread {
    @Override
    public void run() {
        long number = 1L;
        while (true) {
            if (isPrime(number)) {
                System.out.printf("数字 %d 是 质数\n", number);
            }
            /**
             * 每处理完一个数字就检验一下线程是否被中断
             */
            if (isInterrupted()) {
                System.out.println("The PrimeGenerator has been Interrupted");
                return;
            }
            number++;
        }

    }

    /**
     * 校验数字是否为质数的方法
     *
     * @param number
     * @return
     */
    private boolean isPrime(long number) {
        /**
         * 排除1、2、3
         */
        if (number <= 3) {
            return true;
        } else {
            /**
             * 对于大于3的数字
             */
            for (long i = 2; i < number; i++) {
                if (number % i == 0) {
                    return false;
                }
            }
            return true;
        }
    }
}
